from copy import deepcopy


def subject_sort(subjects):
    if len(subjects) <= 1:
        return subjects
    order = {"物理": 1, "历史": 2, "化学": 3, "政治": 4, "生物": 5, "地理": 6, "技术": 7}
    if not all(subject in order.keys() for subject in subjects):
        raise ValueError()
    return sorted(subjects, key=lambda x: order.get(x, float('inf')))


def get_combination(subjects, n):
    if n == 0:
        return []
    if n == 1:
        return [[subject] for subject in subjects]
    if n == 2:
        return [[subjects[0], subjects[1]], [subjects[1], subjects[2]], [subjects[0], subjects[2]]]
    else:
        raise ValueError()

class grade_major_index:
    province_name = ""
    school_name = ""
    major_class = ""
    major_name = ""
    code = ""
    min_rank = 0
    require = {}
    school_province_name = ""
    is_dual_class = 0
    is_985 = 0
    is_211 = 0
    choose1 = "0"
    choose2 = "0"
    choose3 = "0"
    has_plan = 0
    columns = ["province_name", "school_name", "major_class", "major_name", "has_plan", "min_rank",
               "choose1", "choose2", "choose3", "school_province_name", "is_dual_class", "is_985", "is_211"]

    def __init__(self, school_name=""):
        self.school_name = school_name

    def to_df(self):
        d = []
        if self.require["n"] > 0:
            o = get_combination(self.require["o"], self.require["n"])
            for i in range(len(o)):
                r1 = deepcopy(self.require["r"])
                r1.extend(o[i])
                r1 = subject_sort(r1)
                if len(r1) >= 1:
                    self.choose1 = r1[0]
                if len(r1) >= 2:
                    self.choose2 = r1[1]
                if len(r1) >= 3:
                    self.choose3 = r1[2]
                d1 = [self.province_name, self.school_name, self.major_class, self.major_name, self.code, self.min_rank,
                      self.choose1, self.choose2, self.choose3, self.school_province_name, self.is_dual_class,
                      self.is_985, self.is_211, self.has_plan]
                d.append(deepcopy(d1))
        else:
            r1 = deepcopy(self.require["r"])
            r1 = subject_sort(r1)
            if len(r1) >= 1:
                self.choose1 = r1[0]
            if len(r1) >= 2:
                self.choose2 = r1[1]
            if len(r1) >= 3:
                self.choose3 = r1[2]
            d = [[self.province_name, self.school_name, self.major_class, self.major_name, self.code, self.min_rank,
                  self.choose1, self.choose2, self.choose3, self.school_province_name, self.is_dual_class,
                  self.is_985, self.is_211, self.has_plan]]
        return d
